package com.rs.controller;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import com.github.pagehelper.Page;
import com.rs.model.ZhClasses;
import com.rs.model.ZhEmployment;
import com.rs.model.ZhUser;
import com.rs.service.PublicUserMethodsService;
import com.rs.service.ZhClassesService;
import com.rs.service.ZhEmploymentService;
import com.rs.util.SpringContextUtils;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JComboBox;
import javax.swing.JDesktopPane;
import javax.swing.JTextField;
import javax.swing.DefaultComboBoxModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class SelectEmployment extends JInternalFrame {
	
	private JDesktopPane desktopPane_1;
	SelectEmployment ss = this;
	
	private ZhEmploymentService emp = SpringContextUtils.getBean(ZhEmploymentService.class);
	private ZhClassesService classesService = SpringContextUtils.getBean(ZhClassesService.class);
	Map<String, Integer> mapca = new HashMap<>();
	
	private JPanel contentPane;
	private final JScrollPane scrollPane = new JScrollPane();
	private JTable table;
	private JButton button_update;
	private JButton button_upPage;
	private JButton button_nextPage;
	
	private JTextField text_username;
	private JComboBox comboBoxif;
	private JComboBox comboBox_class;
	private JButton button ;
	
	int pages=0;//总页数
	int pageNum=1;//当前页
	int conditions=1; //1为非 条件查询状态 2为条件查询状态
	private JButton button_selelct;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SelectEmployment frame = new SelectEmployment();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public SelectEmployment() {
		
	aa();
	}
	
	public SelectEmployment(JDesktopPane desktopPane_1) {
		this.desktopPane_1=desktopPane_1;
		aa();
		}
	
	void aa(){
		setMaximizable(true);
		setIconifiable(true);
		setClosable(true);
//		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 573, 540);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		scrollPane.setBounds(10, 113, 537, 280);
		contentPane.add(scrollPane);
		
		table = new JTable();
		table.setRowHeight(25);
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
			},
			new String[] {
				"id", "\u59D3\u540D", "\u73ED\u7EA7", "\u4F01\u4E1A\u540D\u79F0", "\u5C31\u4E1A\u5730\u5740", "\u5C31\u4E1A\u85AA\u8D44"
			}
		));
		scrollPane.setViewportView(table);
		
		button_update = new JButton("修改");
		button_update.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				TableModel t = table.getModel();
				int id = (int) t.getValueAt(table.getSelectedRow(), 0);
//				EmploymentInfo em = 
						new  EmploymentInfo(id,desktopPane_1,ss).setVisible(true);
//				em.setVisible(true);
//				desktopPane_1.add(em);
			}
		});
		button_update.setBounds(88, 424, 72, 23);
		contentPane.add(button_update);
		
		button_upPage = new JButton("上一页");
		button_upPage.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				upPage();
			}
		});
		button_upPage.setBounds(334, 424, 93, 23);
		contentPane.add(button_upPage);
		
		button_nextPage = new JButton("下一页");
		button_nextPage.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				nextPage();
			}
		});
		button_nextPage.setBounds(437, 424, 93, 23);
		contentPane.add(button_nextPage);
		
		JButton button_del = new JButton("删除");
		button_del.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				delEmp();
			}
		});
		button_del.setBounds(170, 424, 72, 23);
		contentPane.add(button_del);
		
		JLabel label_userName = new JLabel("姓名：");
		label_userName.setBounds(28, 31, 54, 15);
		contentPane.add(label_userName);
		
		JLabel label_class = new JLabel("班级：");
		label_class.setBounds(195, 31, 54, 15);
		contentPane.add(label_class);
		
		JLabel label = new JLabel("是否就业：");
		label.setBounds(378, 31, 72, 15);
		contentPane.add(label);
		
		comboBox_class = new JComboBox();
		comboBox_class.setBounds(243, 28, 108, 21);
		contentPane.add(comboBox_class);
		comboBox_class.addItem("无");
		
		List<ZhClasses> cl= classesService.listZhClasses();
		for(ZhClasses cc:cl){
		comboBox_class.addItem(cc.getClName());
		mapca.put(cc.getClName(),cc.getClId());
		}
		
		
		comboBoxif = new JComboBox();
		comboBoxif.setModel(new DefaultComboBoxModel(new String[] {"否", "是"}));
		comboBoxif.setBounds(449, 28, 64, 21);
		contentPane.add(comboBoxif);
		
		text_username = new JTextField();
		text_username.setBounds(71, 28, 108, 21);
		contentPane.add(text_username);
		text_username.setColumns(10);
		
		button = new JButton("查询");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				pageNum=1;
				conditions=2;
				nextPage();
			}
		});
		button.setBounds(28, 75, 93, 23);
		contentPane.add(button);
		
		JButton button_sx = new JButton("刷新");
		button_sx.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				shuaxin();
			}
		});
		button_sx.setBounds(454, 80, 93, 23);
		contentPane.add(button_sx);
		
		button_selelct = new JButton("查看");
		button_selelct.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				TableModel t = table.getModel();
				int  id = (int)t.getValueAt(table.getSelectedRow(), 0);
				EmploymentDetailed em = new EmploymentDetailed(id);
				em.setVisible(true);
			}
		});
		button_selelct.setBounds(10, 424, 72, 23);
		contentPane.add(button_selelct);
		nextPage();
		
		
	}
	
	//得到所有毕业 xueyuan
	private List<ZhEmployment> getZhEmployments(){
		Page<ZhEmployment> page= emp.listZhEmployments(pageNum);
		pages = page.getPages();
		return page;
	}
	
	//刷新
	private void shuaxin(){
		pageNum--;
			setTheValue(getZhEmployments());
		pageNum++;
	}
	
	//xia一页
	private void nextPage(){
		if(pageNum >= pages && pageNum !=1 ){
			alert("到底了！");
			return;
		}
		if(conditions ==1){
			setTheValue(getZhEmployments());
		}else if(conditions ==2){
			setTheValue(conditionsSelect());
		}
		pageNum++;
	}
	
	//上一页
	private void upPage(){
		if(pageNum >= 2){
			alert("当前已是第一页！");
			return;
		}
		if(conditions ==1){
			setTheValue(getZhEmployments());
		}else if(conditions ==2){
			setTheValue(conditionsSelect());
		}
		
		pageNum--;
	}
	
	//渲染数据
	private void setTheValue(List<ZhEmployment> list){
		TableModel ta = table.getModel();
		/*int num=0;
		for(ZhEmployment ee:list){
			ta.setValueAt(ee.getEmId(), num, 0);
			ta.setValueAt(ee.getZhUser().getuName(), num, 1);
			ta.setValueAt(ee.getZhClasses().getClName(), num, 2);
			ta.setValueAt(ee.getEmEnterprise(), num, 3);
			ta.setValueAt(ee.getEmAddress(), num, 4);
			ta.setValueAt(ee.getEmSalary(), num, 5);
		}*/
		for(int i=0;i<ta.getRowCount();i++){
			if(i>=list.size()){
				ta.setValueAt(null, i, 0);
				ta.setValueAt(null, i, 1);
				ta.setValueAt(null, i, 2);
				ta.setValueAt(null, i, 3);
				ta.setValueAt(null, i, 4);
				ta.setValueAt(null, i, 5);
				continue;
			}
			ZhEmployment ee = list.get(i);
			ta.setValueAt(ee.getEmId(), i, 0);
			ta.setValueAt(ee.getZhUser().getuName(), i, 1);
			ta.setValueAt(ee.getZhClasses().getClName(), i, 2);
			ta.setValueAt(ee.getEmEnterprise(), i, 3);
			ta.setValueAt(ee.getEmAddress(), i, 4);
			ta.setValueAt(ee.getEmSalary(), i, 5);
		}
	}
	
	private void alert(String aa){
		JOptionPane.showMessageDialog(this, aa);
	}
	
	//删除某一行数据
	private void delEmp(){
		DefaultTableModel ta = (DefaultTableModel)table.getModel();
		Integer id = (Integer) ta.getValueAt(table.getSelectedRow(), 0);
		Integer ifNo=emp.deleteZhEmploymentId(id);
		alert(ifNo>0?"删除成功":"删除失败");
		ta.removeRow(table.getSelectedRow());
		ta.addRow(new String[]{null,null,null,null,null,null});
	}
	
	//条件查询得到的毕业学员的集合
	private List<ZhEmployment> conditionsSelect(){
		ZhEmployment ze = new ZhEmployment();
		if(!text_username.getText().equals("")){
			ZhUser user = new ZhUser();
			user.setuName(text_username.getText());
			ze.setZhUser(user);
		}
		if(!comboBox_class.getSelectedItem().equals("无")){
			ZhClasses cl = new ZhClasses();
			cl.setClId(mapca.get(comboBox_class.getSelectedItem()));
		}
		if(comboBoxif.getSelectedItem().equals("否")){
			ze.setEmEnterprise("notemEnterprise");
		}else if(comboBoxif.getSelectedItem().equals("是")){
			ze.setEmEnterprise("emEnterprise");
		}
		ze.setPageNum(pageNum);
		Page<ZhEmployment> page = emp.selectZhEmployments(ze);
		return page;
	}
}
